home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / misc / sci / RARS_Amiga_2.lha / RARS / cntrlb.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-31  |  3.9 KB  |  132 lines

  1. // CNTRLB.CPP - "driver" function for RARS 
  2. // By Bill Benedict, Feb. 1995
  3. // This version of "Billyboy" should be much more useful :)  It actually
  4. // stays on the track now and runs pretty good on some of the tracks.
  5. // This driver is also heavily modified from the fast 'Kernign' driver.
  6. // This is also my 'working' copy, so there are various bits of code
  7. // that were added and/or commented out along the way.  Feel free to 
  8. // play with and modify this all you want :)
  9. // (see CNTRL0.CPP for better comments)
  10. // adapted to ver. 0.39 3/6/95 by M. Timin
  11.  
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include "car.h"
  16.  
  17. extern double width;
  18. extern const double CARWID;
  19. extern char* glob_name;
  20.  
  21. con_vec cntrlb(situation s)
  22. {
  23.    const char name[] = "Billyboy";
  24.    static int init_flag = 1;
  25.    con_vec result;
  26.    double alpha, vc, rad;
  27.    double steer_gain = .2;
  28.    const double steer_damp = .48;
  29.    const double normalane = 0.0;
  30.    static double vc_mult = 1.0;
  31.    static int lane_time = 0;
  32.    static int lane_time2 = 0;
  33.    static int lane_change = -15;
  34.    static double lane = 0;    
  35.         // determines right-left position on straigtaway
  36.    const double corn_con = 5.9;
  37.    static double corn_spd = 59.0;
  38.  
  39.    if(init_flag)  {
  40.       strcpy(glob_name, name);
  41.       init_flag = 0;
  42.       result.alpha = result.vc = 0;
  43.       return result;
  44.    }
  45.  
  46.   if(stuck(s.backward, s.v,s.vn, s.to_lft,s.to_rgt, &result.alpha,&result.vc))
  47.       return result;
  48.  
  49. if(s.cur_rad == 0.0 && s.nex_rad != 0.0)
  50. corn_spd = corn_con * sqrt(s.nex_rad > 0.0 ? s.nex_rad : -s.nex_rad);
  51. else
  52. corn_spd = corn_con * sqrt(s.cur_rad > 0.0 ? s.cur_rad : -s.cur_rad);
  53. if (corn_spd < 40) corn_spd = 40;
  54.    // maybe choose a different lane: (to help in passing)
  55.    if(!s.dead_ahead) {
  56.       if (lane_time <= 0) {
  57.     lane_time=-1;
  58.     lane = normalane; //+ random(5)*15;
  59.       }
  60.    }
  61.    else if (lane_time2 <= 0){
  62.       // pick a different lane:
  63.       if(lane >= 75)               // pick a new lane somehow:
  64.      lane_change = -15;
  65.       else if(lane <= -75)
  66.      lane_change = 15;
  67.       lane += lane_change;
  68.       if (lane > 90) lane = 90;
  69.       else if (lane < -90) lane = -90;
  70.       lane_time2=20;
  71.       lane_time=100;
  72.    }
  73.    if (lane_time >=0 ){
  74.      lane_time--;
  75.      lane_time2--;
  76.    }
  77. // Audible buzz when the dead_ahead flag is set for this driver
  78. //if(s.dead_ahead) {sound(20);delay(2);nosound();}
  79.    vc_mult=1.0;
  80.  
  81.    if((s.cur_rad == 0) && 
  82.      ((s.nex_rad == 0.0) || 
  83.       (s.to_end > 1.5*s.v) || 
  84.       (s.v < corn_spd*.8))) {
  85.       // in the straight-away and not speeding near a turn
  86.       alpha = .25 * steer_gain * (s.to_lft - s.to_rgt - lane) / width;
  87.       if(s.dead_ahead)
  88.       alpha *= 4.0;
  89.     }
  90.    else {
  91.       if(s.cur_rad == 0) {
  92.         rad=s.nex_rad;
  93.         vc_mult = (s.to_lft < (width/2) ? 
  94.                   (1.1667-s.to_rgt/(3*width)) : 1.0);
  95.       }
  96.       else rad=s.cur_rad;
  97.  
  98.       if(rad > 0.0)   {
  99.     alpha = steer_gain * (3 * (s.to_lft -
  100.              (s.nex_rad < 0.0 ? width/3 : 1.6*CARWID)) / width +
  101.              (s.cur_rad == 0 ? 0 : .05 * width/s.to_rgt));
  102. //      if(s.dead_ahead)
  103. //   alpha *= .7;
  104.  
  105.       }
  106.       else if(rad < 0.0) {
  107.     alpha = -steer_gain * (3 * (s.to_rgt -
  108.              (s.nex_rad > 0.0 ? width/3 : 1.6*CARWID)) / width +
  109.              (s.cur_rad == 0 ? 0 :.05 * width/s.to_lft));
  110. //      if(s.dead_ahead)
  111. //   alpha *= .7;
  112.       }
  113.    }
  114.    alpha -= steer_damp * s.vn / s.v;  
  115.       // This is damping, to prevent oscillation
  116.  
  117.    if(s.cur_rad == 0)         // If we are on a straightaway,
  118.       if((s.to_end > 1.8*s.v)||(s.nex_rad ==0.0))      
  119.                               // if we are far from the end:
  120.         vc = s.v + 85/s.v;    // keep accellerating near full power
  121.       else {                  // otherwise,
  122.         vc = corn_spd;      // maintain cornering speed, braking at first
  123.    // this next formula will not work if a rt. turn follows the straight:
  124. //   alpha += .4 * (s.cur_len/(s.to_end + s.cur_len) - (1/1.25 ));
  125.       }
  126.    else            // if we're in the curve, maintain speed, +
  127.       vc = corn_spd + 5.2 / (s.to_end + .6);
  128.  
  129.    result.vc = vc*vc_mult;   result.alpha = alpha;
  130.    return result;
  131. }                                                               // v;
  132.